<?php
namespace app\app\model;
use think\Db;
use think\Model;

class CardModel extends Model{

    protected $table = "a_card";

    /**
     * 获取个人名片
     * @param $id
     */
    public function getOne($id){
        $result = $this->field('concat("'.config('AUDIO_HEAD').'",audio) audio,address,wechat_num,duties,phone,corporare_name,content')->where('user_id','eq',$id)->find();
        if( !$result ){
            $result = [];
        }
        return $result;
    }

    /**
     * 编辑语音名片信息
     * @param $user_id
     * @param $audio
     * @param $address
     */
    public function saveData($user_id,$audio,$address){
        $card = $this->where('user_id','eq',$user_id)->find();
        //判断是否已有
        if( $card ){
            //更新
            $this->update(array('id'=>$card['id'],'audio'=>$audio,'address'=>$address['address'],'phone'=>$address['phone'],'wechat_num'=>$address['wechat_num'],'duties'=>$address['duties'],'corporare_name'=>$address['corporare_name'],'content'=>$address['content']));
            return getConfig('AUDIO_HEAD').$audio;
        }else{
            $arr = array('user_id'=>$user_id,'audio'=>$audio,'address'=>$address['address'],'phone'=>$address['phone'],'wechat_num'=>$address['wechat_num'],'duties'=>$address['duties'],'corporare_name'=>$address['corporare_name'],'content'=>$address['content']);
            $this->save($arr);
            return getConfig('AUDIO_HEAD').$audio;
        }
    }
    
    
    public function saveCardByCardId($id,$data){
        if(!$id || !is_numeric($id)){
            exception('传递的ID不合法');
        }
        $this->update(array('id'=>$id,'address'=>$data['address'],'phone'=>$data['phone'],'wechat_num'=>$data['wechat_num'],'duties'=>$data['duties'],'corporare_name'=>$data['corporare_name'],'content'=>$data['content']));
    }

}